import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'logic.dart';

class LoginPage extends StatelessWidget {
  LoginPage({Key? key}) : super(key: key);

  final LoginLogic logic = Get.put(LoginLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 24.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: 80.h),
                _buildHeader(),
                SizedBox(height: 40.h),
                _buildLoginForm(),
                SizedBox(height: 20.h),
                _buildRegisterButton(),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildHeader() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '欢迎使用',
          style: TextStyle(
            fontSize: 28.sp,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
        ),
        SizedBox(height: 8.h),
        Text(
          '乡村振兴APP',
          style: TextStyle(
            fontSize: 20.sp,
            color: Colors.grey[600],
          ),
        ),
      ],
    );
  }

  Widget _buildLoginForm() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // Username field
        TextField(
          controller: logic.usernameController,
          decoration: InputDecoration(
            labelText: '用户名',
            prefixIcon: Icon(Icons.person),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10.r),
            ),
          ),
        ),
        SizedBox(height: 16.h),
        // Password field
        TextField(
          controller: logic.passwordController,
          obscureText: true,
          decoration: InputDecoration(
            labelText: '密码',
            prefixIcon: Icon(Icons.lock),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10.r),
            ),
          ),
        ),
        SizedBox(height: 24.h),
        // Login button
        Obx(() => SizedBox(
          width: double.infinity,
          height: 50.h,
          child: ElevatedButton(
            onPressed: logic.isLoading.value ? null : logic.login,
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.green,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.r),
              ),
            ),
            child: logic.isLoading.value
                ? CircularProgressIndicator(color: Colors.white)
                : Text(
                    '登录',
                    style: TextStyle(
                      fontSize: 16.sp,
                      color: Colors.white,
                    ),
                  ),
          ),
        )),
      ],
    );
  }

  Widget _buildRegisterButton() {
    return Center(
      child: TextButton(
        onPressed: logic.goToRegister,
        child: Text(
          '没有账号？点击注册',
          style: TextStyle(
            fontSize: 14.sp,
            color: Colors.green,
          ),
        ),
      ),
    );
  }
}
